package com.gisuni.sddc.platformManager.controller;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.nutz.log.Log;
import org.nutz.log.Logs;
import org.apache.http.HttpRequest;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.GET;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import com.gisuni.sddc.platformManager.model.MessageInfo;
import com.gisuni.sddc.platformManager.model.Response;
import com.gisuni.sddc.platformManager.service.MessageInfoService;

@IocBean(singleton=false)
@At("/client/backgroundManagement/MessageController")
public class MessageInfoController {
	private Log log = Logs.getLog(MessageInfoController.class);
	
    @Inject 
    private MessageInfoService messageInfoService;
    
    @At("/allShowMessage")
    @GET
    @POST
    @Ok("json")
    public Object allShowMessage(){
    	List<MessageInfo> messList=new ArrayList<MessageInfo>();
    	try{
    		messList=messageInfoService.getShowAllMessage();
    	}catch(Exception e){
    		log.error(e);
    	}
    	if(messList.size()==0){
    		MessageInfo info=new MessageInfo();
    		info.setTitle("暂无通知");
    		messList.add(info);
    	}
    		return messList;
    }
    @At("/allMessage")
    @GET
    @POST
    @Ok("json")
    public Object allMessage(){
    	List<MessageInfo> messList=new ArrayList<MessageInfo>();
    	try{
    		messList=messageInfoService.getAllMessage();
    	}catch(Exception e){
    		log.error(e);
    	}
    	return messList;
    }
    @At("/addNewMessage")
    @POST
    @Ok("json")
    public Object addNewMessage(HttpServletRequest request){
    	Response res=new Response();
    	res.setSuccess(false);
    	try{
    		res=messageInfoService.addNewMessage(request);
    	}catch(Exception e){
    		log.error(e);
    		res.setMsg("操作失败");
    	}
		return res;
    }
    @At("/getMessageById/")
    @POST
    @GET
    @Ok("json")
    public MessageInfo getMessageById(HttpServletRequest request){
    	String id=request.getParameter("id");
    	MessageInfo mess=messageInfoService.getMessageById(id);
		return mess;
    }
    @At("/updateMessage/")
    @POST
    @GET
    @Ok("json")
    public Object updateMessage(@Param(value="id")String id,@Param(value="title")String title,@Param(value="content")String content){
    	Response res=new Response();
    	res.setSuccess(false);
    	try{
    	 boolean msg=messageInfoService.updateMessage(id,title,content);
    	 if(msg==true){
    		 res.setSuccess(true);
    		 res.setMsg("保存成功");
    	 }else{
    		 res.setMsg("保存失败");
    	 }
    	}catch(Exception e){
    		log.error(e);
    		res.setMsg("操作失败");
    	}
		return res;
    }
    @At("/deleteMessage/")
    @POST
    @GET
    @Ok("json")
    public Object deleteMessage(@Param(value="id")String id){
    	Response res=new Response();
    	res.setSuccess(false);
    	try{
       	 boolean msg=messageInfoService.deleteMessage(id);
       	 if(msg==true){
       		 res.setSuccess(true);
       		 res.setMsg("删除成功");
       	 }else{
       		 res.setMsg("删除失败");
       	 }
       	}catch(Exception e){
       		log.error(e);
       		res.setMsg("操作失败");
       	}
   		return res;
    }
    @At("/updateMessageStatus/")
    @POST
    @GET
    @Ok("json")
    public Object updateMessageStatus(@Param(value="id")String id,@Param(value="status")String flag){
    	Response res=new Response();
    	res.setSuccess(false);
    	try{
    		boolean msg=messageInfoService.updateMessageStatus(id,flag);
    		if(msg==true){
    			res.setSuccess(true);
          		res.setMsg("修改成功");
    		}else{
    			res.setMsg("修改失败");
    		}
    	}catch(Exception e){
    		log.error(e);
       		res.setMsg("操作失败");
    	}
    	return res;
    }
    
    
}
